package com.example.shushiyunsst.fragment.main.mine.child

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.mine.child.bean.MessageBean
import com.example.shushiyunsst.fragment.main.mine.child.bean.SpotMessageBean
import com.example.shushiyunsst.util.SPOT_MESSAGE
import com.example.wallet.db.UserInfoManager
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/***
 * 站内信
 * */
@Route(path = SPOT_MESSAGE)
class SpotMessageFragment : SimpleListFragment<SpotMessageBean>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        mHeader.setBackgroundResource(R.color.bg_color_0)
        addHeader(R.layout.comment_header_layout)
        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.title = "站内信息"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mRecyclerView.clipToPadding=false
        mRecyclerView.setPadding(0,PixUtils.dp2px(10),0,0)
    }

    override fun getItemLayout(): Int {
        return R.layout.spot_message_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: SpotMessageBean?) {
        helper?.setText(R.id.title, item?.content)
            ?.setText(R.id.time, item?.createTime)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(SpotMessageBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        parseDate("")

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex,
            "uid" to userInfo?.uid,
        )
        RestClient.builder()
            .url(R.string.queryMessageList)
            .raw(JSON.toJSONString(map))
            .success { parseDate(it) }
            .error { _, _ -> onFailed() }
            .failure { onFailed() }
            .build()
            .post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"]
        val listStr =list.toString()
        val array=JSON.parseArray(listStr,SpotMessageBean::class.java)
        loadListDate(array)
    }
}